<?php
namespace backend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\base\Model;
use yii\web\IdentityInterface;

/**
 * Departments model
 *显示字段

 */
class Departments extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;

    /*public $grade;
    public $departName;
    public $departPcount;
    public $parentId;
    public $isDelete;
    public $isEnable;*/



    /**
     * @inheritdoc
     */
     
    public static function tableName()
    {
        return '{{%Departments}}';
    }
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function rules(){
        return [
            [['grade','departName','departPcount','parentId','isDelete','isEnable'],'required']
        ];
    }
    /**
     * @inheritdoc
     */
    	public function behaviors()
	{
		return [
				'timestamp' => [
						'class' => 'yii\behaviors\TimestampBehavior',
						'attributes' => [
								ActiveRecord::EVENT_BEFORE_INSERT => [
										
										
								],
								ActiveRecord::EVENT_BEFORE_UPDATE => [
										 
								] 
						] 
				] 
		];
	}

    /**
     * @inheritdoc
     */
   /* public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
        ];
    }
   */

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['userName' => $username]);
    }
    /**
     * 获取部门列表
     */
    public static function getDepartmentsList($grade,$parentId){
        $departmentList=static::find()->where(['isDelete'=>0,'grade'=>$grade,'parentId'=>$parentId])->asArray()->all();
        return $departmentList;

    }

    /**
     * 增加部门
     */
    public static function addDepartment(){

    }
    public function getDepartName(){

    }
    public function getParentId(){

    }
    /**
     * 和自身做一个绑定
     */
    /*public function getDepartments(){
        return static::hasOne(static::className(),['id'=>'parentId']);
    }*/
    /**
     * 调取部门列表
     */
    public static function getAllDeparmentList(){
        $departmentsList=Departments::getDepartmentsList(1,0);
        foreach($departmentsList as $key=>$value){
            $subList1=Departments::getDepartmentsList(2,$value['id']);


            foreach($subList1 as $key1=>$value1){
                $subList2=Departments::getDepartmentsList(3,$value1['id']);

                foreach($subList2 as $key2=>$value2){
                    $subList3=Departments::getDepartmentsList(4,$value2['id']);

                    foreach($subList3 as $key3=>$value3){
                        $subList4=Departments::getDepartmentsList(5,$value3['id']);

                        /* foreach($subList4 as $key4=>$value4){
                             $subList5=Departments::getDepartmentsList(6,$value4['id']);
                            // $subList4[$key4]['fiveGrade']=$subList5;
                         }*/
                        $subList3[$key3]['fiveGrade']=$subList4;
                    }
                    $subList2[$key2]['fourGrade']=$subList3;
                }
                $subList1[$key1]['threeGrade']=$subList2;
            }

            $departmentsList[$key]['twoGrade']=$subList1;
        }
        return $departmentsList;
    }
}
